-
Notifications
You must be signed in to change notification settings - Fork 395
Reject duplicate JSON members when parsing signatures #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This is a combination of json.Unmarshal + validateExactMapKeys
except using the paranoidUnmarshalJSONObject parser (refusing duplicate
and unknown fields),
+ unmarshaling into typed values instead of a map[string]interface{},
i.e. making int64Field/mapField/stringField unnecessary.
This does not add any user yet.
Signed-off-by: Miloslav Trmač <[email protected]>
…ctUnmarshalJSON Signed-off-by: Miloslav Trmač <[email protected]>
i.e. validateExactMapKeys, int64Field, mapField, stringField. untrustedSignature.strictUnmarshalJSON no longer uses them. The test scenarios are covered by tests of untrustedSignature.strictUnmarshalJSON already (except for testing the full range of int64 timestamps). Signed-off-by: Miloslav Trmač <[email protected]>
A test was referring to the old "specific" name for the "transports" member. Signed-off-by: Miloslav Trmač <[email protected]>
| // (including duplicated keys, unrecognized keys, and non-matching types). Each of the fields in exactFields | ||
| // must be present exactly once, and none other fields are accepted. | ||
| func paranoidUnmarshalJSONObjectExactFields(data []byte, exactFields map[string]interface{}) error { | ||
| seenKeys := map[string]struct{}{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be more idiomatic, but a map[string]bool is better suited here and allows to do
If seenkeys[i]...
W/o playing around with !ok below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I somewhat prefer the struct{} to make the type closer to the true type (set of string), see also earlier conversation in #223 , and perhaps earlier.
I don’t feel all that strongly about this, but if you can live with it, I’ll merge as is.
In most of the cases, the original code did not explicitly require that a (string) field is present, but empty strings were later rejected (because the "type" field was required to be equal to a constant, or because parsing a docker/reference would fail). Either way, there were already tests verifying that missing fields are rejected; now the code is just a bit more explicit about it. Signed-off-by: Miloslav Trmač <[email protected]>
9353672 to
3994e56
Compare
Instead of relying on
json.Unmarshalinto amap[string]interface{}, and dealing with field presence and correct types manually, build a helper based onparanoidUnmarshalJSONObjectswhich verifies field presence automatically, relies onjson.Unmarshalfor enforcing types of unstructured values, and newly rejects JSON objects with duplicate members.Besides the primary benefit, rejecting ambiguous signatures, this allows us to get rid of the infrastructure for safely manually parsing
map[string]interface{}. The newparanoidUnmarshalJSONObjectExactFieldscan also be used to simplifypolicy.jsonparsing a bit.Also includes one unrelated test bug fix.
See the individual commits for more details.